<template>
<div class="user">
    <!--添加页面-->
    <div class="common-level-rail">
      <el-button
        size="small"
        type="primary"
        icon="Plus"
        @click="addClick()"
        v-auth="'/page/page/add'"
        >{{$t('page.add_page')}}</el-button
      >
    </div>

    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column
            prop="pageId"
            :label="$t('page.page_id')"
            width="80"
          ></el-table-column>
          <el-table-column prop="pageName" :label="$t('page.page_name')"></el-table-column>
          <el-table-column prop="createTime" :label="$t('page.add_time')"></el-table-column>
          <el-table-column prop="updateTime" :label="$t('page.update_time')"></el-table-column>
          <el-table-column fixed="right" :label="$t('page.action')" width="90">
            <template #default="scope">
              <el-button
                @click="editClick(scope.row.pageId)"
                type="text"
                size="small"
                v-auth="'/page/page/edit'"
                >{{$t('page.edit')}}</el-button
              >
              <el-button
                @click="deleteClick(scope.row.pageId)"
                type="text"
                size="small"
                v-auth="'/page/page/delete'"
                >{{$t('page.delete')}} 
</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import PageApi from "@/api/page.js";
export default {
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },
    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = {};
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      PageApi.PageList(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.list.records;
        self.totalDataNumber = res.data.list.total;
      }).catch(error => {});
    },
    /*删除数据*/
    deleteClick(row) {
      let self = this;
      ElMessageBox.confirm($t("page.delete_confirm"), $t("page.prompt"), {
        confirmButtonText: $t("page.confirm"),
        cancelButtonText: $t("page.cancel"),
        type: "warning"
      }).then(() => {
        self.loading = true;
        PageApi.deletePage({
          pageId: row
        }, true).then(data => {
          if (data.code == 1) {
            self.loading = false;
            ElMessage({
              message: $t("page.delete_success"),
              type: "success"
            });
            self.getTableList();
          } else {
            self.loading = false;
          }
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {});
    },
    /*添加页面*/
    addClick() {
      this.$router.push("/page/page/add");
    },
    /*设为首页*/
    setHomeClick(pageId) {
      let self = this;
      ElMessageBox.confirm($t("page.set_default_page_confirm"), $t("page.prompt"), {
        confirmButtonText: $t("page.confirm"),
        cancelButtonText: $t("page.cancel"),
        type: "warning"
      }).then(() => {
        self.loading = true;
        PageApi.setHome({
          pageId: pageId
        }, true).then(data => {
          self.loading = false;
          if (data.code == 1) {
            ElMessage({
              message: $t("page.set_success"),
              type: "success"
            });
            self.getTableList();
          } else {
            ElMessage.error($t("page.error_message"));
          }
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {});
    },
    /*编辑*/
    editClick(pageId) {
      let self = this;
      self.$router.push({
        path: "/page/page/edit",
        query: {
          pageId: pageId
        }
      });
    }
  }
};
</script>

